//
//  WYCRepaymentController.m
//  51还款
//
//  Created by 中企互联 on 2017/10/30.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCRepaymentController.h"
#import "WYCRepaymentCell.h"
#import "WYCCreditCardController.h"
#import "WYCDebitCardController.h"
#import "WYCBillModel.h"
#import "WYCBillFrameModel.h"
#import "WYCAddCreditController.h"
#import "WYCAddBankCardController.h"

@interface WYCRepaymentController ()<UIScrollViewDelegate,WYCCreditCardControllerDelegate,WYCDebitCardControllerDelegate>

@property(nonatomic,strong)CALayer *bottomLineLayer;

/** 底部所有的内容 */
@property (nonatomic, weak) UIScrollView  * contenView;

/** 底部所有的标签 */
@property (nonatomic, weak) UIView  * titlesView;

//表视图
@property(nonatomic,strong)UITableView * tableView;

/** 数组*/
@property(nonatomic,strong)NSMutableArray * billarray;

/** 数组*/
@property(nonatomic,strong)NSMutableArray * Dillarray;


/** 取反 */
@property (nonatomic, weak) UIButton  * selectedButton;

/** 取反 */
@property (nonatomic, strong) UIButton  * Button;

/** 取反 */
@property (nonatomic, strong) NSString  * creditStr;

@property (nonatomic,strong) NSMutableArray * arr;


@end

@implementation WYCRepaymentController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = WYCGlobalBg;
//    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
//    [self.navigationController.navigationBar setBarTintColor:WYCMain];
    
    [self setupNav];
    //左侧按钮
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"add" hight:@"add" target:self action:@selector(photoClick)];
    
    
    [self setupChildVces];
    
    [self setupScrollView];
    
    //列表数组
    self.billarray = [[NSMutableArray alloc] init];
    
    self.Dillarray = [[NSMutableArray alloc] init];
    
    [self setuprequset];
    
    
    
    
}

- (void)setuprequset{
    
    NSString *getcardlist = [NSString stringWithFormat:@"%@token=%@",usergetDebitCard,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:getcardlist params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue] ==0) {
            NSArray * resultArray = responseObject[@"result"];
            for (NSDictionary * dict in resultArray) {
                
                WYCBillModel * model = [WYCBillModel mj_objectWithKeyValues:dict];
                if ([model.isHideCard isEqualToString:@"1"]) {
                    continue;
                }else{
                    WYCBillFrameModel * BillFrameModel = [[WYCBillFrameModel alloc] init];
                    BillFrameModel.Billmodel = model;
                    [self.Dillarray addObject:BillFrameModel];
                    [self.Button setTitle:[NSString stringWithFormat:@"借记卡(%lu)",(unsigned long)self.Dillarray.count] forState:UIControlStateNormal];
                }
            }
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
        }
        
        [self.tableView.mj_header endRefreshing];
        
        WYCLog(@"---------%lu",(unsigned long)self.Dillarray.count);
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
        [self.tableView.mj_header endRefreshing];
    }];
    
}
- (void)setdebit:(NSString *)debitStr{
    //    self.creditStr = debitStr;
    self.Button = self.arr[1];
   
    
}
- (void)setVlus:(NSString *)Str{
    
    self.creditStr = Str;
    self.Button = self.arr[0];
    [self.Button setTitle:[NSString stringWithFormat:@"信用卡(%@)",self.creditStr] forState:UIControlStateNormal];
}
- (void)setupChildVces{
    WYCCreditCardController * CreditCard = [[WYCCreditCardController alloc] init];
    CreditCard.delegate = self;
    CreditCard.title = @"信用卡";
    [self addChildViewController:CreditCard];
    
    WYCDebitCardController * DebitCard = [[WYCDebitCardController alloc] init];
    DebitCard.delegate = self;
    DebitCard.title = @"借记卡";
    [self addChildViewController:DebitCard];
    
}
#pragma make ------>>界面Nav
- (void)setupNav{
    
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = WYCGlobalBg;
    titlesView.frame = CGRectMake(0, NavHeight, ScreenWidth, 250 * px);
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    
    NSArray * titlearray = @[[NSString stringWithFormat:@"信用卡(%lu)",(unsigned long)self.billarray.count],[NSString stringWithFormat:@"借记卡(%lu)",(unsigned long)self.Dillarray.count]];
    CGFloat width = (ScreenWidth - 80 * px) / 2 ;
    CGFloat height = 150 * px;
    
    
    NSMutableArray * arr = [[NSMutableArray alloc] init];
    self.arr = arr;
    for (int i =0; i < titlearray.count; i++) {
        self.Button = [[UIButton alloc] init];
        
        
        [arr addObject:self.Button];
        
        self.Button.tag = i;
        self.Button.height = height;
        self.Button.width = width;
        self.Button.x = (ScreenWidth - 80 * px) / 2 * i + 40 * px;
        self.Button.y =  50 * px;
        [self.Button setTitle:titlearray[i] forState:UIControlStateNormal];
        [self.Button layoutIfNeeded];//强制布局
        //        button.backgroundColor = [UIColor whiteColor];
        [self.Button setBackgroundImage:[UIImage imageNamed:@"left_w"] forState:UIControlStateNormal];
        
        [self.Button setBackgroundImage:[UIImage imageNamed:@"left_r"] forState:UIControlStateDisabled];
        self.Button.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        [self.Button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [self.Button setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
        self.Button.titleLabel.font = [UIFont systemFontOfSize:BigMiddleFont];
        [self.Button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:self.Button];
        
        if (i == 0) {
            
            self.Button.enabled = NO;
            self.selectedButton = self.Button;
            
            [self titleClick:self.Button];
        }else {
            [self.Button setBackgroundImage:[UIImage imageNamed:@"right_w"] forState:UIControlStateNormal];
            
            [self.Button setBackgroundImage:[UIImage imageNamed:@"right_r"] forState:UIControlStateDisabled];
            
        }
        
    }
    
    
}
- (void)titleClick:(UIButton *)button
{
    //修改按钮状态
    self.selectedButton.enabled = YES;
    //    button.backgroundColor = [UIColor redColor];
    button.enabled = NO;
    //    self.selectedButton.backgroundColor = [UIColor whiteColor];
    self.selectedButton = button;
    
    //滚动
    CGPoint offset = self.contenView.contentOffset;
    offset.x = button.tag * self.contenView.width;
    [self.contenView setContentOffset:offset animated:YES];
}

/**
 *底部的scrollview
 */
- (void)setupScrollView
{
    //不自动调整inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView * contenView = [[UIScrollView alloc]init];
    contenView.frame = self.view.bounds;
    contenView.delegate = self;
    contenView.pagingEnabled = YES;
    
    [self.view insertSubview:contenView atIndex:0];
    contenView.contentSize = CGSizeMake(contenView.width * self.childViewControllers.count,0 );
    
    self.contenView = contenView;
    //
    //    //添加第一个控制器view
    [self scrollViewDidEndScrollingAnimation:contenView];
}
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
    //当前的索引
    //长度NSInteger * interger;
    int index = scrollView.contentOffset.x / scrollView.width;
    
    //取出子控制器
    UITableViewController * vc = self.childViewControllers[index];
    vc.tableView.x = scrollView.contentOffset.x;
    vc.tableView.y = 220 * px;//tableview的高度默认为20
    vc.tableView.width = ScreenWidth;
    vc.tableView.height = ScreenHeight - 220 * px;//tableview的底部
    //设置内边距
    
    //        CGFloat bottom = self.tabBarController.tabBar.height;
    //        CGFloat top = CGRectGetMaxY(self.titlesView.frame) + 64;
    //        vc.tableView.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    //设置滚动条的内边距
    //        vc.tableView.scrollIndicatorInsets = vc.tableView.contentInset;
    [scrollView addSubview:vc.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //点击按钮
    int index = scrollView.contentOffset.x / scrollView.width;
    [self titleClick:self.titlesView.subviews[index]];
    
    
    // 添加子控制器的view
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

#pragma make ----->>添加事件
- (void)photoClick{
    WYCLogFunc;
    if (self.selectedButton.tag == 0) {
        WYCAddCreditController * addCredit = [[WYCAddCreditController alloc] init];
        [self.navigationController pushViewController:addCredit animated:NO];
    }else{
        WYCAddBankCardController * bankcard = [[WYCAddBankCardController alloc] init];
        [self.navigationController pushViewController:bankcard animated:NO];
        
    }
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    
    
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCmainColor;
    [self.view addSubview:NavView];
    
    
    
        CALayer *bottomLineLayer = [[CALayer alloc] init];
        bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
        bottomLineLayer.backgroundColor = WYCRGBColor(191, 191, 196).CGColor;
        [NavView.layer addSublayer:bottomLineLayer];
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:backbutton];
    //返回按钮
    UIButton *  addbutton = [[UIButton alloc] init];
    [addbutton setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [addbutton addTarget:self action:@selector(photoClick) forControlEvents:UIControlEventTouchUpInside];
    addbutton.frame = CGRectMake(NavView.width - 96 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:addbutton];
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"卡包";
    [NavView addSubview:NavRigsterLabel];
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end

